extern crate rand;
use std::io;
use rand::Rng;
use std::cmp::Ordering;

fn main() {
    println!("猜数字游戏!");
    let rand_number = rand::thread_rng().gen_range(1,101);
    //开始循环
    loop {
        println!("请输入你猜的数字:");
        let mut guess = String::new();      //创建了一个新的，空的 String
        io::stdin().read_line(&mut guess)          //接收用户输入
            .expect("输入类型错误");

        let guess:u32 = match guess.trim().parse() {
            Ok(num) => num,
            Err(_) => continue,
        };

        println!("你猜的: {}", guess);
        match guess.cmp(&rand_number) {
            Ordering::Greater   => println!("很遗憾，太大了！"),
            Ordering::Less      => println!("很遗憾，太小了！"),
            Ordering::Equal     => {
                println!("恭喜！你猜对啦！" );
                println!("游戏结束");
                break; //退出循环
            }
        }
    }
}
